package com.hzy.fivepalace.db.service;

import com.hzy.fivepalace.db.dao.DtsGoodsCommissionConfigMapper;
import com.hzy.fivepalace.db.dao.DtsVipConfigMapper;
import com.hzy.fivepalace.db.domain.DtsGoodsCommissionConfig;
import com.hzy.fivepalace.db.domain.DtsGoodsCommissionConfigExample;
import com.hzy.fivepalace.db.domain.DtsVipConfig;
import com.hzy.fivepalace.db.domain.DtsVipConfigExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 会员配置服务
 */
@Service
public class DtsVipConfigService {

    @Resource
    private DtsVipConfigMapper dtsVipConfigMapper;

    @Resource
    private DtsGoodsCommissionConfigMapper goodsCommissionConfigMapper;

    /**
     * 获取商品佣金比例配置
     * @param vipType 会员类型
     * @param goodsPoroductId 货品id
     * @return
     */
    public DtsVipConfig getGoodsExtendScale(Integer vipType,Integer goodsPoroductId){
        //先查询该会员类型默认的商品佣金比例配置信息
        DtsVipConfig vipConfig=getVipConfigByVipType(vipType);
        if(null!=vipConfig){
            //再查询该会员对应的货品的佣金比例
            DtsGoodsCommissionConfigExample example=new DtsGoodsCommissionConfigExample();
            example.setOrderByClause("id desc");
            example.createCriteria().andVipTypeEqualTo(vipType).andGoodsPoroductIdEqualTo(goodsPoroductId);
            DtsGoodsCommissionConfig goodsCommissionConfig= goodsCommissionConfigMapper.selectOneByExample(example);
            if(null!=goodsCommissionConfig&&null!=goodsCommissionConfig.getGoodsOneLevelExtendScale()&&goodsCommissionConfig.getGoodsOneLevelExtendScale().intValue()!=-1098){
                vipConfig.setGoodsOneLevelExtendScale(goodsCommissionConfig.getGoodsOneLevelExtendScale());

            }
            if(null!=goodsCommissionConfig&&null!=goodsCommissionConfig.getGoodsTwoLevelExtendScale()&&goodsCommissionConfig.getGoodsTwoLevelExtendScale().intValue()!=-1098){
                vipConfig.setGoodsTwoLevelExtendScale(goodsCommissionConfig.getGoodsTwoLevelExtendScale());
            }
        }

        return vipConfig;
    }


    /**
     * 根据会员类型查询
     * @param vipType
     * @return
     */
    public DtsVipConfig getVipConfigByVipType(Integer vipType){
        DtsVipConfigExample example=new DtsVipConfigExample();
        example.setOrderByClause("id desc");
        example.createCriteria().andVipTypeEqualTo(vipType);
        return dtsVipConfigMapper.selectOneByExample(example);
    }
}
